package com.beeasy.hzbpm.bean;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.github.llyb120.nami.core.Async;
import com.github.llyb120.nami.core.Config;
import com.github.llyb120.json.Json;
import com.github.llyb120.json.Obj;
import org.beetl.sql.core.SQLBatchReady;
import org.beetl.sql.core.SQLReady;
import org.bson.Document;

import java.lang.annotation.Documented;
import java.util.*;
import java.util.stream.Collectors;

import static com.github.llyb120.json.Json.o;
import static com.beeasy.hzbpm.bean.MongoService.db;

public class Notice {


    public static void sendSystem(String uid, String msg, String id){
        sendSystem(Collections.singletonList(uid), msg, id);
        //@fixme
//        Async.execute(() -> {
//            String finalMsg = String.format(msg);
//            db.getCollection("system_notice")
//                .insertOne(o(
//                    "id", IdUtil.objectId(),
//                    "add_time", new Date(),
//                    "content", finalMsg,
//                    "state", "UNREAD",
//                    "type", "BPM-MESSAGE",
//                    "user_id", uid,
//                    "bind_data", o("id", id)
//                ).toBson());
////            sqlManager.executeUpdate(new SQLReady("insert into T_SYSTEM_NOTICE(id,add_time,content,state,type,user_id,bind_data) values (?,?,?,?,?,?,?)", snowflake.nextId(), new Date(), finalMsg, "UNREAD", "BPM-MESSAGE", uid,Json.stringify(o("id", id))));
//        });
    }

    public static void sendSystem(Collection<String> uids, String msg, String id){
        //@fixme
        Async.execute(() -> {
            String finalMsg = String.format(msg);
            Date date = new Date();
            //push message
            String url = Config.config.var.ss("push-notice-url");
            if(!url.isEmpty()){
                /*Obj params = o(
                        "uids", uids,
                        "content", msg,
                        "timestamp", System.currentTimeMillis(),
                        "id",id
                );
                String rep = HttpUtil.get(url, o(
                        "param", params
                ));*/

                HttpUtil.createPost(url)
                    .header("Content-Type", "application/json; charset=utf-8")
                    .body(o(
                        "uids", uids,
                        "content", msg,
                        "timestamp", date,
                        "id",id
                    ).toString())
                    .timeout(30000)
                    .executeAsync();
            }
            //入库
            List<Document> params = uids.stream()
                    .map(uid -> {
                        return o(
                            "id", IdUtil.objectId(),
                            "add_time", date,
                            "content", finalMsg,
                            "state", "UNREAD",
                            "type", "BPM-MESSAGE",
                            "user_id", uid,
                            "bind_data", o("id", id)
                        ).toBson();
                    })
                    .collect(Collectors.toList());
            db.getCollection("system_notice")
                .insertMany(params);
//            SQLBatchReady batch = new SQLBatchReady("insert into T_SYSTEM_NOTICE(id,add_time,content,state,type,user_id,bind_data) values (?,?,?,?,?,?,?)", params);
//            sqlManager.executeBatchUpdate(batch);
        });


    }

}
